package team.archai.nexus.boot.web.filters;

import org.springframework.stereotype.Component;
import team.archai.nexus.boot.tool.Tool;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @description: requestId filter
 * @author: lishibin
 * @create: 2020-07-11 01:59
 **/
@Component
public class RequestIdFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 初始化逻辑，如果需要
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String requestId = Tool.uuid();
        // 将 requestId 添加到请求属性中
        httpRequest.setAttribute("requestId", requestId);
        // 继续过滤链
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        // 清理逻辑，如果需要
    }
}
